//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Tablero.h"
//---------------------------------------------------------------------------
Tablero::Tablero(double x, double y, double z, int nx, int ny, int nz){
        _ancho=x;       _alto=y;        _largo=z;
        _nAncho=nx;     _nAlto=ny;      _nLargo=nz;

        //_nCara=(2*(nx-1)*(nz-1))+(4*(nx-1)*(ny-1));
        _nCara=(2*nx*nz)+(4*nx*ny);
        _cara = new Cara*[_nCara];

        _nVertice=(2*(nx+1)*(nz+1))+(4*(nx+1)*(ny+1));  //2 caras horizontales y 2 verticales
        _vertice=new PV3D*[_nVertice];

        _nNormal=_nVertice;//_nNormal=_nCara;
        _normal=new PV3D*[_nNormal];

        PV3D* nnewell;

        int indice_vertice, indice_cara, total_vertices, total_caras;
        double px,py,pz,inc_x,inc_y,inc_z;
        inc_x=_ancho/(double)nx;        inc_y=_alto/(double)ny;        inc_z=_largo/(double)nz;

        //Horizontales superiores
        py=_alto/(double)2; pz=-_largo/(double)2;
        for(int i=0; i<=nz; i++){
                px=-_ancho/(double)2;
                for(int j=0; j<=nx; j++){
                        indice_vertice=i*(nz+1)+j;
                        _vertice[indice_vertice]=new PV3D(px,py,pz,1);
                        _normal[indice_vertice]=new PV3D(0,1,0,0);

                        if(i>0 && j>0){
                                //indice_cara = (i-1)*(nz-1)+(j-1);
                                indice_cara = (i-1)*nz+(j-1);
                                VerticeNormal** vertice_normal = new VerticeNormal*[4];
                                vertice_normal[0]=new VerticeNormal(indice_vertice,indice_vertice);
                                vertice_normal[1]=new VerticeNormal(indice_vertice-nx-1,indice_vertice-nx-1);
                                vertice_normal[2]=new VerticeNormal(indice_vertice-nx-2,indice_vertice-nx-2);
                                vertice_normal[3]=new VerticeNormal(indice_vertice-1,indice_vertice-1);

                                _cara[indice_cara]=new Cara(4,vertice_normal);
                        }
                        px+=inc_x;
                }
                pz+=inc_z;
        }
        total_vertices=indice_vertice+1;
        total_caras=indice_cara+1;
        //Horizontales inferiores
        py=-_alto/(double)2; pz=-_largo/(double)2;
        for(int i=0;i<=nz; i++){
                px=-_ancho/(double)2;
                for(int j=0; j<=nx; j++){
                        indice_vertice=i*(nz+1)+j+total_vertices;//indice_vertice=i*nz+j+total_vertices;
                        _vertice[indice_vertice]=new PV3D(px,py,pz,1);
                        _normal[indice_vertice]=new PV3D(0,1,0,0);
                        //_normal[indice_vertice]->normaliza();

                        if(i>0 && j>0){
                                indice_cara = (i-1)*nz+(j-1)+total_caras;
                                VerticeNormal** vertice_normal = new VerticeNormal*[4];
                                vertice_normal[0]=new VerticeNormal(indice_vertice,indice_vertice);
                                vertice_normal[1]=new VerticeNormal(indice_vertice-nx-1,indice_vertice-nx-1);
                                vertice_normal[2]=new VerticeNormal(indice_vertice-nx-2,indice_vertice-nx-2);
                                vertice_normal[3]=new VerticeNormal(indice_vertice-1,indice_vertice-1);

                                _cara[indice_cara]=new Cara(4,vertice_normal);
                        }
                        px+=inc_x;
                }
                pz+=inc_z;
        }

        total_vertices=indice_vertice+1;
        total_caras=indice_cara+1;

        //px=_ancho/(double)2;    py=_alto/(double)2;     pz=_largo/(double)2;
        //Vertices frontales

        px=_ancho/(double)2; pz=-_largo/(double)2;
        for(int i=0; i<=nz; i++){
                py=-_alto/(double)2;
                for(int j=0; j<=ny; j++){
                        indice_vertice=i*(nz+1)+j+total_vertices;
                        _vertice[indice_vertice]=new PV3D(px,py,pz,1);
                        _normal[indice_vertice]=new PV3D(0,0,1,0);

                        if(i>0 && j>0){
                                indice_cara = (i-1)*nz+(j-1)+total_caras;
                                VerticeNormal** vertice_normal = new VerticeNormal*[4];
                                vertice_normal[0]=new VerticeNormal(indice_vertice,indice_vertice);
                                vertice_normal[1]=new VerticeNormal(indice_vertice-nx-1,indice_vertice-nx-1);
                                vertice_normal[2]=new VerticeNormal(indice_vertice-nx-2,indice_vertice-nx-2);
                                vertice_normal[3]=new VerticeNormal(indice_vertice-1,indice_vertice-1);

                                _cara[indice_cara]=new Cara(4,vertice_normal);
                        }
                        py+=inc_y;
                }
                pz+=inc_z;
        }

        total_vertices=indice_vertice+1;
        total_caras=indice_cara+1;
        //px=_ancho/(double)2;    py=_alto/(double)2;     pz=-_largo/(double)2;
        //Vertices posteriores
        px=-_ancho/(double)2; pz=-_largo/(double)2;
        for(int i=0; i<=nz; i++){
                py=-_alto/(double)2;
                for(int j=0; j<=ny; j++){
                        indice_vertice=i*(nz+1)+j+total_vertices;
                        _vertice[indice_vertice]=new PV3D(px,py,pz,1);
                        _normal[indice_vertice]=new PV3D(0,0,-1,0);

                        if(i>0 && j>0){
                                indice_cara = (i-1)*nz+(j-1)+total_caras;
                                VerticeNormal** vertice_normal = new VerticeNormal*[4];
                                vertice_normal[0]=new VerticeNormal(indice_vertice,indice_vertice);
                                vertice_normal[1]=new VerticeNormal(indice_vertice-nx-1,indice_vertice-nx-1);
                                vertice_normal[2]=new VerticeNormal(indice_vertice-nx-2,indice_vertice-nx-2);
                                vertice_normal[3]=new VerticeNormal(indice_vertice-1,indice_vertice-1);

                                _cara[indice_cara]=new Cara(4,vertice_normal);
                        }
                        py+=inc_y;
                }
                pz+=inc_z;
        }
        
        total_vertices=indice_vertice+1;
        total_caras=indice_cara+1;
        //px=_ancho/(double)2;    py=_alto/(double)2;     pz=_largo/(double)2;
        //Vertices laterales drch
        pz=-_largo/(double)2; px=-_ancho/(double)2;
        for(int i=0; i<=nx; i++){
                py=-_alto/(double)2;
                for(int j=0; j<=ny; j++){
                        indice_vertice=i*(nz+1)+j+total_vertices;
                        _vertice[indice_vertice]=new PV3D(px,py,pz,1);
                        _normal[indice_vertice]=new PV3D(1,0,0,0);

                        if(i>0 && j>0){
                                indice_cara = (i-1)*nz+(j-1)+total_caras;
                                VerticeNormal** vertice_normal = new VerticeNormal*[4];
                                vertice_normal[0]=new VerticeNormal(indice_vertice,indice_vertice);
                                vertice_normal[1]=new VerticeNormal(indice_vertice-nx-1,indice_vertice-nx-1);
                                vertice_normal[2]=new VerticeNormal(indice_vertice-nx-2,indice_vertice-nx-2);
                                vertice_normal[3]=new VerticeNormal(indice_vertice-1,indice_vertice-1);

                                _cara[indice_cara]=new Cara(4,vertice_normal);
                        }
                        py+=inc_y;
                }
                px+=inc_x;
        }

        total_vertices=indice_vertice+1;
        total_caras=indice_cara+1;
        //px=-_ancho/(double)2;    py=_alto/(double)2;     pz=_largo/(double)2;
        //Vertices laterales drch
        pz=_largo/(double)2; px=-_ancho/(double)2;
        for(int i=0; i<=nx; i++){
                py=-_alto/(double)2;
                for(int j=0; j<=ny; j++){
                        indice_vertice=i*(nz+1)+j+total_vertices;
                        _vertice[indice_vertice]=new PV3D(px,py,pz,1);
                        _normal[indice_vertice]=new PV3D(-1,0,0,0);

                        if(i>0 && j>0){
                                indice_cara = (i-1)*nz+(j-1)+total_caras;
                                VerticeNormal** vertice_normal = new VerticeNormal*[4];
                                vertice_normal[0]=new VerticeNormal(indice_vertice,indice_vertice);
                                vertice_normal[1]=new VerticeNormal(indice_vertice-nx-1,indice_vertice-nx-1);
                                vertice_normal[2]=new VerticeNormal(indice_vertice-nx-2,indice_vertice-nx-2);
                                vertice_normal[3]=new VerticeNormal(indice_vertice-1,indice_vertice-1);

                                _cara[indice_cara]=new Cara(4,vertice_normal);
                        }
                        py+=inc_y;
                }
                px+=inc_x;
        }

        _textura=-1;
}

Tablero::~Tablero(){
}
//---------------------------------------------------------------------------

#pragma package(smart_init)

